﻿
@{
    ViewBag.Title = "MemberGroupEdit";
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}

<script>

    var _mode = "create";//modify
    var _validator;
    var _memberGroup = null;

    $(document).ready(function ()
    {
        $("[keyenter]").keypress(function (e)
        {
            if (e.keyCode == 13)
            {
                save();
            }
        });

        _validator = $("#form").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtName": "required"
            },
            messages: {
                "txtName": "请输入分组名称；"
            }
        });

        load();
    });

    function load()
    {
        var id = getQueryString("id");
        if (id == undefined || id == "")
        {
            return;
        }

        _mode = "modify";
        $("#spanTitle").html("修改分组");

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        $.ajax({
            url: "/Api/Member/GetMemberGroup?id=" + id,
            type: "POST",
            dataType: "json",
            success: function (data, status, jqXHR)
            {
                layer.close(loadLayerIndex);
                if (data.Success)
                {
                    _memberGroup = data.Data;
                    $("#txtId").val(_memberGroup.Id);
                    $("#txtName").val(_memberGroup.Name);

                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function save()
    {
        if (_validator.form() == false)
        {
            return;
        }

        //var index = parent.layer.getFrameIndex(window.name);
        //parent.insertReplyTextContent($("#txtName").val(), index);

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var url = "/Api/Member/CreateMemberGroup";

        if (_memberGroup == undefined || _memberGroup == null)
        {
            _memberGroup = new Object();
        } else
        {
            url = "/Api/Member/UpdateMemberGroup";
        }

        _memberGroup.Name = $("#txtName").val();

        $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: JSON.stringify(_memberGroup),
            success: function (data, status, jqXHR)
            {
                layer.close(loadLayerIndex);
                if (data.Success)
                {
                    var index = parent.layer.getFrameIndex(window.name);
                    // parent.layer.close(index);

                    if (_mode == "create")
                    {
                        parent.loadMemberGroupListAndCloseLayer(index);
                    } else
                    {
                        parent.loadMemberGroupListAndCloseLayer(index, _memberGroup.Name);
                    }
                } else
                {
                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest)
            {
                layer.close(loadLayerIndex);
                alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function cancel()
    {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }
</script>


<div style="margin-left:20px; margin-right:20px; margin-top:20px;">
    <span id="spanTitle" class="font_black_24">新建分组</span>
</div>

<div style=" background-color:#ccc; margin-left:20px; margin-right:20px; margin-top:10px; height:1px;">

</div>

<div style=" position:absolute; overflow:auto ;margin-top:25px;left:30px; right:30px; bottom:60px; top:50px; ">

    <form id="form">
        <input type="hidden" id="txtId" />
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td>
                    <input type="text" name="txtName" maxlength="6" class="input_18" id="txtName" keyenter />
                </td>
            </tr>

        </table>


    </form>

</div>

<div style=" background-color:#ccc; position:absolute; bottom:55px; left:20px;right:20px; height:1px;">

</div>

<div style="position:absolute; bottom:15px; left:20px;right:20px;">

    <!--
            <div style="float:left;">
                <input name="btnSave" type="button" class="btn_red" id="btnSave" value="删 除" />
            </div>-->

    <div style="float:right">
        <input name="btnSave" type="button" class="btn_blue" id="btnSave" value="保 存" onclick="save()" />
        <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="cancel()" />
    </div>
    <div style="clear:both">
    </div>
</div>
